#include <iostream>
#include <vector>
#include <chrono>

// 定义一个向上对齐到指定字节边界的宏
#define ALIGN(x) alignas(x)

void gaussianElimination(std::vector<std::vector<double>>& matrix) {
    int n = matrix.size();
    
    for (int i = 0; i < n; i++) {
        double pivot = matrix[i][i];
        
        // 将当前行的数据向上对齐到16字节边界
        ALIGN(16) double* currentRow = &matrix[i][i];
        
        for (int j = i; j < n + 1; j++) {
            currentRow[j - i] /= pivot;
        }
        
        for (int k = 0; k < n; k++) {
            if (k != i) {
                double factor = matrix[k][i];
                
                // 将要修改的行的数据向上对齐到16字节边界
                ALIGN(16) double* rowToModify = &matrix[k][i];
                
                for (int j = i; j < n + 1; j++) {
                    rowToModify[j - i] -= factor * currentRow[j - i];
                }
            }
        }
    }
}

int main() {
    std::vector<int> dataSizes = {100, 300, 500, 800, 1000, 1500, 2000, 3000};
    
    for (int size : dataSizes) {
        // Generate a random matrix
        std::vector<std::vector<double>> matrix(size, std::vector<double>(size + 1));
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < size + 1; j++) {
                matrix[i][j] = static_cast<double>(rand()) / RAND_MAX;
            }
        }
        
        // Perform Gaussian elimination and measure the execution time
        auto startTime = std::chrono::high_resolution_clock::now();
        gaussianElimination(matrix);
        auto endTime = std::chrono::high_resolution_clock::now();
        auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(endTime - startTime).count();
        
        // Print the result
        std::cout << "Data size: " << size << "x" << size << "\n";
        std::cout << "Execution time: " << duration << " milliseconds\n\n";
    }
    
    return 0;
}
